
/**
 * Date:2019年6月4日下午3:08:29
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.im.client.ui.list;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.SwingConstants;

import com.xue.im.client.core.IMClientContext;
import com.xue.im.client.ui.ImageScale;
import com.xue.im.common.IMUtil;
import com.xue.im.common.vo.User;

/**
 * Description: 用户list内容渲染 <br/>
 * Date: 2019年6月4日 下午3:08:29 <br/>
 * 
 * @author xuejianxin
 */
public class UserCellRender extends DefaultListCellRenderer {
	private static final long serialVersionUID = 1L;
	private static ImageIcon woman = ImageScale.getImage(IMClientContext.woman, 60, 60);
	private static ImageIcon man = ImageScale.getImage(IMClientContext.man, 60, 60);

	@Override
	public Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index,
			boolean isSelected, boolean cellHasFocus) {

		if (value instanceof User) {
			// System.out.println(index +":"+ isSelected + cellHasFocus);
			User obj = (User) value;
			try {
				ImageIcon icon;
				if (obj.getSex() == 0) {
					icon = woman;
				} else {
					icon = man;
				}

				setIcon(icon);
				String color = obj.getStatus() == IMUtil.USER_STATUS_ONLINE ? " color='blue' " : " ";
				String msgCount = obj.getMsgCount() > 0 ? "<br/><b color='red'> 新消息" + obj.getMsgCount() + "条</b>" : "";
				String text1 = "<html><body " + color + ">" + obj.getUsername() + "<br/>" + obj.getIp() + msgCount
						+ "</body></html>";

				setText(text1);

				// 设置文本的水平和垂直位置:比如（右上）
				setVerticalTextPosition(SwingConstants.TOP);
				setHorizontalTextPosition(SwingConstants.RIGHT);

				// 设置背景色
				if (isSelected) {
					setBackground(Color.ORANGE);
				} else {
					setBackground(Color.white);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return this;
	}
}
